﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using Recomendaciones.Entidades;

namespace Recomendaciones.Paginas
{
    public partial class Busqueda : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                llenarRepeater();
        }

        public void llenarRepeater()
        {
            try
            {
                ItemServices servicioItems = new ItemServices();
                servicio servicio = new servicio();
                List<Item> items = null;
                ParDatos resultado = null;
                int usuarioId = -1;

                CurrentPage.Value = Request.QueryString["pag"];

                int startRecord = ((int.Parse(CurrentPage.Value) - 1) * int.Parse(PageSize.Value)) + 1;
                int endRecord = startRecord - 1 + (int.Parse(PageSize.Value));

                if (HttpContext.Current.Session["usuarioID"] != null && Convert.ToBoolean(Request.QueryString["user"]))
                    usuarioId = ((Usuario)HttpContext.Current.Session["usuarioID"]).Id;

                resultado = servicioItems.BusquedaPorCategoria(Request.QueryString["item"], Request.QueryString["categoria"], Request.QueryString["valor"], Convert.ToInt32(Request.QueryString["pd"]), Convert.ToInt32(Request.QueryString["ph"]), usuarioId, startRecord, endRecord);
                items = (List<Item>)resultado.DatoUno;
                TotalSize.Value = resultado.DatoDos.ToString();


                if (resultado != null)
                {
                    Repeater1.DataSource = items;
                    Repeater1.DataBind();
                }
                else
                {
                    TotalSize.Value = Convert.ToString(0);
                    Repeater1.DataSource = null;
                }

            }
            catch (Exception ex)
            {
                LogErrorServices logErrorService = new LogErrorServices();
                logErrorService.InsertarLogError(ex.GetType().ToString(), ex.StackTrace, ex.Message, ex.GetBaseException().ToString(), ex.Source);
            }
        }

    }
}